package com.nit.generate;

import com.google.common.collect.Lists;
import com.nit.tools.NitStringTools;
import com.nit.tools.loop.NitLoopTools;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.List;

/**
 * 域名 生成器
 *
 * @Auth: 逆天开源-逍遥哥哥
 * @link: 1715656022@qq.com
 * @Date: 2023-07-21
 */
public class NitDomainGenerator {

    public static void main(String[] args) {
        NitLoopTools.loop(4, () -> {
            String tmp = NitDomainGenerator.generate();
            System.out.println("随机域名:" + tmp + "<br>");
        });
    }


    /**
     * 域名后缀
     */
    private static final List<String> DOMAIN_SUFFIX_LIST = Lists.newArrayList("com", "org", "net", "cn", "io", "im", "info", "mobi", "biz", "pro", "us", "me", "top", "tv", "cc");

    public static String generate() {
        return generate(NitNumberGenerator.generateInt(2, 30));
    }

    /**
     * 生成随机的域名
     *
     * @param maxLength 域名最大长度(最小长度为2)
     * @return 随机域名
     */
    public static String generate(int maxLength) {
        String domain = RandomStringUtils.randomAlphanumeric(2, maxLength + 1) + "." +
                DOMAIN_SUFFIX_LIST.get(NitNumberGenerator.generateInt(0, DOMAIN_SUFFIX_LIST.size()));
        return domain.toLowerCase();
    }

    /**
     * @param prefix 前缀  http或者 https
     * @param suffix 文件后缀
     * @return url
     */
    public static String generateWithSuffix(String prefix, String suffix) {
        if (NitStringTools.isBlank(prefix)) {
            prefix = "http://";
        }
        if (NitStringTools.isBlank(suffix)) {
            prefix = "jpg";
        }
        String domain = prefix + generate() + "/" + NitUUIDGenerator.generate() + "." + suffix;
        return domain.toLowerCase();
    }

}
